package com.itheima.service.impl;

import com.itheima.commons.pojo.CommonEntity;
import com.itheima.service.ElasticsearchIndexService;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
* @Class: ElasticsearchIndexServiceImpl
* @Package com.itheima.service.impl
* @Description:索引接口实现类
* @Company: http://www.itheima.com/
*/
@Service
public class ElasticsearchIndexServiceImpl implements ElasticsearchIndexService {
    @Resource
    private RestHighLevelClient client;
    /*
    * @Description: 新增索引+映射
    * @Method: addIndexAndMapping
    * @Param: [commonEntity]
    * @Update:
    * @since: 1.0.0
    * @Return: boolean
    *
    */
    @Override
    public boolean addIndexAndMapping(CommonEntity commonEntity) throws Exception {
        //创建索引请求
        CreateIndexRequest request=new CreateIndexRequest(commonEntity.getIndexName());
        //获取前端参数
        Map<String,Object> map=commonEntity.getMap();
        //循环外层的setting和mapping
        for(Map.Entry<String,Object> entry:map.entrySet()){

            if("settings".equals(entry.getKey())){
                if(entry.getValue()  instanceof  Map  &&  ((Map) entry.getValue()).size()>0){
                    request.settings((Map<String, Object>) entry.getValue());
                }
            }

            if("mapping".equals(entry.getKey())){
                if(entry.getValue()  instanceof  Map  &&  ((Map) entry.getValue()).size()>0){
                    request.mapping((Map<String, Object>) entry.getValue());
                }
            }
        }
        //创建索引操作的客户端
        IndicesClient indicesClient=client.indices();
        //创建响应对象
        CreateIndexResponse createIndexResponse=indicesClient.create(request,RequestOptions.DEFAULT);
        return createIndexResponse.isAcknowledged();
    }
}
